import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite, tween, Vec3 } from 'cc';
import { BaseComponent } from '../../../../extensions/cocos-scaffold-3x/assets/ui/base/BaseComponent';
import { JC } from '../m/JokerStorage';
import { setNodeScale } from '../../../../extensions/cocos-scaffold-3x/assets/common/Utils';
import { ViewJoker } from '../ViewJoker';
const { ccclass, property } = _decorator;

export class PokerCardProp {
    constructor(
        public card: JC.PokerCard,
        public viewJocker: ViewJoker
    ) { }
}

@ccclass('PokerCard')
export class PokerCard extends BaseComponent<PokerCardProp> {

    @property(Label)
    lbNum: Label = null


    @property(Label)
    lbValue: Label = null


    @property(Sprite)
    sprColorBg: Sprite = null

    @property(Prefab)
    addScorePrefab: Prefab = null


    static getCardColor(type: number) {
        switch (type) {
            case 0:
                return new Color(255, 100, 100)
            case 1:
                return new Color(100, 255, 100)
            case 2:
                return new Color(100, 100, 255)
            case 3:
                return new Color(223, 200, 100)
            default:
                return new Color(255, 255, 255)
        }
    }

    onInit(): void {
        this.lbValue.string = `${this.prop.card.value}`
        switch (this.prop.card.point) {
            case 11:
                this.lbNum.string = `${"J"}`
                break;
            case 12:
                this.lbNum.string = `${"Q"}`
                break;
            case 13:
                this.lbNum.string = `${"K"}`
                break;
            case 14:
                this.lbNum.string = `${"A"}`
                break;
            default:
                this.lbNum.string = `${this.prop.card.point}`
                break;
        }
        this.sprColorBg.color = PokerCard.getCardColor(this.prop.card.suit)
    }

    async playAddScore() {
        return new Promise((resolve, reject) => {
            setNodeScale(this.node, 1.4)
            const addScoreNode = instantiate(this.addScorePrefab)
            setNodeScale(addScoreNode, 0)
            this.node.addChild(addScoreNode)
            addScoreNode.getComponent(Label).string = `+${this.prop.card.value}`
            this.prop.viewJocker.pokerGame.currentStage.base += this.prop.card.value
            tween(addScoreNode).to(0.2, { scale: new Vec3(1, 1, 1) }, { easing: "expoOut" }).delay(0.3).to(0.2, { scale: new Vec3(0, 0, 0) }, { easing: "expoOut" })
                .call(() => {
                    setNodeScale(this.node, 1.2)

                    resolve(null)
                })
                .start()
        })


    }


    start() {

    }

    update(deltaTime: number) {

    }

    isSelected = false


    checkSelect() {
        if (this.isSelected) {
            this.isSelected = !this.isSelected
        } else {
            const pokerGame = this.prop.viewJocker.pokerGame
            if (!this.isSelected && this.prop.viewJocker.handCardPanel.getSelectedCards().length < pokerGame.playCardNumLimit) {
                this.isSelected = true
            }
        }

    }

    onTouch() {
        if (this.prop.viewJocker.waittingCount) {
            return
        }
        this.checkSelect()

        setNodeScale(this.node, this.isSelected ? 1.2 : 1)
    }
}

